#!/bin/sh
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - nr_task
# - size
# END ENVIRONMENT VARIABLES

USEMEM=$BENCHMARK_ROOT/vm-scalability/usemem

if [ -z "$size" ]; then
	echo "size not specified" >&2
	exit 1
fi

swaptotal=$(grep SwapTotal /proc/meminfo |awk '{print $2}')
if [ "$swaptotal" -eq 0 ]; then
        echo "No swap space. Please setup swap first" >&2
        exit 1
fi

# start N detached processes and do write first to consume all memory
# then wait for SIGUSR1 to resume to do read
$USEMEM -n $nr_task -W -d -p $TMP/pidfile $((size / nr_task)) > $TMP_RESULT_ROOT/swapin-setup

# Now that the N detached processes are done, start another process
# to consume memory to push pages allocated by the first N processes
# to swap.
# Also, redirect its output to /dev/null so that it won't output any
# statistics related information
memtotal=$(grep MemTotal /proc/meminfo |awk '{print $2}')
memtotal=$((memtotal << 10))
$USEMEM $memtotal>/dev/null
# do it again to push more memory to swap
$USEMEM $memtotal>/dev/null
